package org.doubango.FrCall.Utils;

import java.util.ArrayList;
import java.util.List;

import org.doubango.FrCall.model.RecentRoster;
import org.doubango.ngn.utils.NgnDataBaseHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class RosterStoreManager extends NgnDataBaseHelper {
	
		private final static String DATABASE_NAME = "recentDB";
		private final static int DATABASE_VERSION = 1;
		private final static String TABLE_NAME = TABLE_RECENT_FRIENDS_NAME;


		public RosterStoreManager(Context context) {
			super(context, DATABASE_NAME, DATABASE_VERSION, TABLE_ST);
			// TODO Auto-generated constructor stub
		}

		public long insert(RecentRoster user) {
			if(checkExist(user))
			{
				return this.update(user);
			}
			SQLiteDatabase db = this.mDataBaseOpenHelper
					.getWritableDatabase();
			ContentValues cv = new ContentValues();
			cv.put(FIELD_USER_ID, user.getUserId());
			cv.put(FIELD_FRIEND_ID,user.getFriends());
			cv.put(FIELD_FRIEND_MESSAGE,user.getRecentMessage());
			long time = System.currentTimeMillis();
			cv.put(FIELD_FRIEND_TIME,time);
			long row = db.insert(TABLE_NAME, null, cv);
			return row;
		}

		public long delete(String userId) {
			SQLiteDatabase db = this.getmDataBaseOpenHelper()
			.getWritableDatabase();
			String where = FIELD_USER_ID + "=? AND "+FIELD_FRIEND_ID+"=?";
			String[] whereValue = { userId };
			return db.delete(TABLE_NAME, where, whereValue);
		}

		public long update(RecentRoster user) {
			SQLiteDatabase db = this.getmDataBaseOpenHelper()
			.getWritableDatabase();
			String where = FIELD_USER_ID + "=? AND "+FIELD_FRIEND_ID+"=?";
			String[] whereValue = {user.getUserId(),user.getFriends()};
			ContentValues cv = new ContentValues();
			cv.put(FIELD_FRIEND_MESSAGE,user.getRecentMessage());
			long time = System.currentTimeMillis();
			cv.put(FIELD_FRIEND_TIME,time);
			return db.update(TABLE_NAME, cv, where, whereValue);
		}
		public Cursor select() {
			SQLiteDatabase db = this.getmDataBaseOpenHelper().getReadableDatabase();
			Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null,
					FIELD_FRIEND_TIME+" desc");
			return cursor;
		}
		public List<RecentRoster> getUserList() {
			List<RecentRoster> userlist = new ArrayList<RecentRoster>();
			Cursor cur = select();
			cur.moveToFirst();
			while (!cur.isAfterLast() && cur.getCount() > 0) {
				RecentRoster user = new RecentRoster();
				user.setUserId(cur.getString(cur.getColumnIndex(FIELD_USER_ID)));
				String friendId = cur.getString(cur.getColumnIndex(FIELD_FRIEND_ID));
				String message = cur.getString(cur.getColumnIndex(FIELD_FRIEND_MESSAGE));
				user.setFriends(friendId);
				user.setRecentMessage(message);
				userlist.add(user);
				cur.moveToNext();
			};
			cur.close();
			return userlist;
		}

		public RecentRoster getUserByUserID(RecentRoster user1) {
			SQLiteDatabase db = this.getmDataBaseOpenHelper().getReadableDatabase();;
			Cursor cur = db
					.query(TABLE_NAME, null, FIELD_USER_ID + "=? AND "+FIELD_FRIEND_ID+"=?",
							new String[] { user1.getUserId(),user1.getFriends() }, null, null, " " + FIELD_FRIEND_TIME
									+ " desc");
			cur.moveToFirst();
			if( cur.getCount() <= 0 || cur.isLast() )
			{
				return null;
			}
			RecentRoster user = new RecentRoster();
			user.setUserId(cur.getString(cur.getColumnIndex(FIELD_USER_ID)));
			String friendId = cur.getString(cur.getColumnIndex(FIELD_FRIEND_ID));
			user.setFriends(friendId);
			cur.close();
			db.close();
			return user;
		}

		public boolean checkExist(RecentRoster user) {
			SQLiteDatabase db = this.getmDataBaseOpenHelper().getReadableDatabase();;
			Cursor cur = db
					.query(TABLE_NAME, null, FIELD_USER_ID + "=? AND "+FIELD_FRIEND_ID+"=?",
							new String[] { user.getUserId(),user.getFriends() }, null, null, " " + FIELD_ID
									+ " desc");
			cur.moveToFirst();
			if( cur.getCount() <= 0)
			{
				cur.close();
				return !true;
			}
			cur.close();
			return true;
		}
	}
